package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.Employee;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.pubClass.RMainUtils;


/**
 * Created by XXX on 2017/11/29.
 */

public class SelectLostRRAdapter extends RecyclerView.Adapter<SelectLostRRAdapter.RHolder>{

    private Context mContext;
    private List<tempLostRR> rrList;
    private List<Employee> employeeList;
    private OnItemClickListener itemClickListener;
    private List<RMain> rMains;

    public SelectLostRRAdapter(Context context, List<tempLostRR> List,List<Employee> emps,List<RMain> rMains,OnItemClickListener itemClickListener){
        mContext = context;
        rrList = List;
        employeeList = emps;
        this.itemClickListener = itemClickListener;
        this.rMains = rMains;
    }

    @Override
    public RHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.mulity_item_spinner_view,parent,false);
        return new RHolder(view);
    }

    @Override
    public void onBindViewHolder(final RHolder holder, int position) {
        final tempLostRR rr = rrList.get(position);
        holder.X_item_text.setText(rr.getRowName());
        if (rr.isFbIsCheck()){
            holder.X_checkbox.setChecked(true);
        }else{
            holder.X_checkbox.setChecked(false);
        }
        holder.taskScore.setText("得分："+rr.getScore());

        holder.X_Rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.X_checkbox.isChecked()){
                    holder.X_checkbox.setChecked(false);
                    rr.setFbIsCheck(false);
                }else{
                    holder.X_checkbox.setChecked(true);
                    rr.setFbIsCheck(true);
                }
                itemClickListener.onItemClick(rr);

            }
        });
        //获取应的评分的数据
            RMain.TablesBean.RowsBean rowsBean = RMainUtils.getInstance().getRowByMInId(rMains,rr.getRRowId());
            for (RMain.TablesBean.RowsBean.RItemsBean item:rowsBean.getRItems()){
                if (item.getID() == rr.getSelectItemId()){
                    holder.taskScore.setText("得分："+item.getName()+"("+rr.getScore()+")");
                    break;
                }
            }
        //执行人数据
        List<String> data_list = new ArrayList<>();
        if(employeeList!=null&&employeeList.size()>0){
            for (Employee emp:employeeList){
                data_list.add(emp.getFirstName());
            }
            //适配器
            ArrayAdapter<String> arr_adapter= new ArrayAdapter<>(mContext, R.layout.myspinner, data_list);
            //设置样式
            arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            holder.mySpinner.setAdapter(arr_adapter);
            //设置默认值
            if (!TextUtils.isEmpty(rr.getXindongId()+"")){
                for (int k =0;k<employeeList.size();k++){
                    if (employeeList.get(k).getID()==rr.getXindongId()){
                        holder.mySpinner.setSelection(k);
                    }
                }
            }else{
                holder.mySpinner.setSelection(0);
                rr.setXindongId(employeeList.get(0).getID());
            }
            holder.mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (employeeList!=null){
                        rr.setXindongId(employeeList.get(position).getID());
                        holder.mySpinner.setSelection(position);
                    }
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }else{
            holder.mySpinner.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
        return rrList!=null?rrList.size():0;
    }

    class RHolder extends RecyclerView.ViewHolder {
        RelativeLayout X_Rel;
        CheckBox X_checkbox;
        TextView X_item_text;
        TextView taskScore;
        AppCompatSpinner mySpinner;
        private RHolder(View itemView) {
            super(itemView);
            X_Rel = (RelativeLayout) itemView.findViewById(R.id.X_Rel);
            X_checkbox = (CheckBox) itemView.findViewById(R.id.X_checkbox);
            X_item_text = (TextView) itemView.findViewById(R.id.X_item_text);
            taskScore = itemView.findViewById(R.id.taskScore);
            mySpinner = (AppCompatSpinner)itemView.findViewById(R.id.mySpinner);
        }
    }
    //返回选取的数据
    public List<tempLostRR> getCheckItemList(){
        List<tempLostRR> checkList = new ArrayList<>();
        if (rrList!=null){
            for (tempLostRR rr:rrList){
              if (rr.isFbIsCheck()){
                  checkList.add(rr);
              }
            }
        }
        return checkList;

    }
    public List<tempLostRR> getCurrentList(){
        return rrList;
    }
    //全选操作
    public void setAllCheck(){
        List<tempLostRR> rrArrayList = new ArrayList<>();
        if (rrList!=null){
            for (tempLostRR rr:rrList){
                if (!rr.isFbIsCheck()){
                    rr.setFbIsCheck(true);
                }
            }
            notifyDataSetChanged();
        }
    }

    public int getSelectCount(){
        int count = 0;
        if (rrList!=null){
            for (tempLostRR rr:rrList){
                if (rr.isFbIsCheck()){
                    count++;
                }
            }
        }
        return count;
    }
    //全部选操作
    public void disCheckAll(){
        List<tempLostRR> rrArrayList = new ArrayList<>();
        if (rrList!=null){
            for (tempLostRR rr:rrList){
                if (rr.isFbIsCheck()){
                    rr.setFbIsCheck(false);
                }
            }
            notifyDataSetChanged();
        }

    }

    public String getCheckString(){
        int selectCount = 0;
        int allCount = 0;
        if (rrList!=null){
            allCount = rrList.size();
            for (tempLostRR rr:rrList){
                if (rr.isFbIsCheck()){
                    selectCount++;
                }
            }
        }
        return selectCount + "/" + allCount;
    }

    public interface OnItemClickListener{
        void onItemClick(tempLostRR rr);
    }

}
